//
//  MKMapDrawLineViewController.m
//  UILbrary
//
//  Created by mac on 17/3/14.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "MKMapDrawLineViewController.h"
#import "GradientPolylineOverlay.h"
#import "GradientPolylineRenderer.h"
#import <MapKit/MapKit.h>
@interface MKMapDrawLineViewController ()<MKMapViewDelegate>
@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) GradientPolylineOverlay *polyline;
/** 播放轨迹按钮 */
@property (nonatomic, strong) UIButton *playButton;
@end

@implementation MKMapDrawLineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"画历史轨迹";
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.view addSubview:self.mapView];
    [self.view addSubview:self.playButton];
    
}
- (void)playButtonClick{
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(39.90112, 116.411777);
    MKCoordinateRegion region = MKCoordinateRegionMake(center, MKCoordinateSpanMake(0.025, 0.025));
    [self.mapView setRegion:region animated:YES];
    NSMutableArray *mutabelArr = [NSMutableArray array];
    for (int cout=0; cout <=100; cout ++ ) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        NSString *latitude = [NSString stringWithFormat:@"%lf",39.90112 + 0.02*cout];
        NSString *longitude = [NSString stringWithFormat:@"%lf",116.411777 + 0.02*cout];
        dic[@"latitude"] = latitude;
        dic[@"longitude"] = longitude;
        dic[@"speed"] = @"100";
        [mutabelArr addObject:dic];
    }
    NSMutableArray *smoothTrackArray = mutabelArr;//[self smoothTrack];
    double count = smoothTrackArray.count;
    CLLocationCoordinate2D *points;
    float *velocity;
    points = malloc(sizeof(CLLocationCoordinate2D)*count);
    velocity = malloc(sizeof(float)*count);
    
    for(int i=0;i<count;i++){
        NSDictionary *dic = [smoothTrackArray objectAtIndex:i];
        CLLocationDegrees latitude  = [dic[@"latitude"] doubleValue];
        CLLocationDegrees longitude = [dic[@"longitude"] doubleValue];
        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
        velocity[i] = [dic[@"speed"] doubleValue];
        points[i] = coordinate;
    }
    
    self.polyline = [[GradientPolylineOverlay alloc] initWithPoints:points velocity:velocity count:count];
    for (int cout = 0; cout < 100; cout ++) {
        [self.mapView addOverlay:self.polyline level:1];
    }
    
}
#pragma mark -
#pragma mark - MKMap Delegate
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay{
    if([overlay isKindOfClass:[GradientPolylineOverlay class]]){
        //轨迹
        GradientPolylineRenderer *polylineRenderer = [[GradientPolylineRenderer alloc] initWithOverlay:overlay];
        polylineRenderer.lineWidth = 8.f;
        return polylineRenderer;
    }
    return nil;
}

#pragma mark - lazy methods
- (MKMapView *)mapView{
    if (!_mapView) {
        _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
        _mapView.delegate = self;
        _mapView.zoomEnabled = YES;
        _mapView.scrollEnabled = YES;
        _mapView.rotateEnabled = YES;
        _mapView.showsScale = YES;
        _mapView.showsCompass = YES;
        _mapView.showsUserLocation = YES;
    }
    return _mapView;
}

- (UIButton *)playButton{
    if (!_playButton) {
        _playButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
        _playButton.center  =  self.view.center;
        [_playButton setBackgroundColor:[UIColor orangeColor]];
        [_playButton setTitle:@"播放" forState:UIControlStateNormal];
        [_playButton addTarget:self action:@selector(playButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playButton;
}
@end
